package com.cmcc.waf.report.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * <p>
 *
 * @author liang_jinxin
 * @date 2023/4/17
 */
public class DateUtils {
    public static final DateTimeFormatter DEFAULT_DATETIME_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static final DateTimeFormatter DEFAULT_DATE_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    public static void main(String[] args) {
        System.out.println(swapGMT(new Date()));
        String dateString = "20220101";
        System.out.println(dateSwap_(dateString));
    }

    public static String swapGMT(Date dateCST) {
        DateFormat df = new SimpleDateFormat("MMM d HH:mm:ss yyyy z", Locale.ENGLISH);
        df.setTimeZone(TimeZone.getTimeZone("GMT"));
        return df.format(dateCST);
    }

    /***
     * yyyyMMdd转成yyyy_MM_dd
     */
    public static String dateSwap_(String dateString) {
        String outputDate = "";
        DateFormat inputFormat = new SimpleDateFormat("yyyyMMdd");
        DateFormat outputFormat = new SimpleDateFormat("yyyy_MM_dd");
        try {
            Date date = inputFormat.parse(dateString);
            outputDate = outputFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return outputDate;
    }

}
